package cn.itcast.goods.admin.category.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.goods.category.domain.Category;
import cn.itcast.goods.category.service.CategoryService;
import cn.mytools.commons.CommonUtils;
import cn.mytools.servlet.BaseServlet;

public class AdminCategoryServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -246455486708522794L;
	private CategoryService categoryService = new CategoryService();

	/**
	 * 查询所有分类
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Category> categoryList = categoryService.findAll();
		req.setAttribute("parents", categoryList);
		return "f:/adminjsps/admin/category/list.jsp";
	}

	/**
	 * 添加一级分类
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addOneLevel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Category category = CommonUtils.toBean(req.getParameterMap(), Category.class);
		category.setCid(CommonUtils.uuid());
		categoryService.addCategory(category);
		return findAll(req, resp);
	}

	/**
	 * 添加二级分类第一步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addTwoLevelPre(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String cid = req.getParameter("cid");
		List<Category> parents = categoryService.findAllParents();
		req.setAttribute("parents", parents);
		req.setAttribute("cid", cid);
		return "f:/adminjsps/admin/category/add2.jsp";
	}

	/**
	 * 添加二级分类第二步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addTwoLevel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Category parent = new Category();
		String pid = req.getParameter("pid");
		parent.setCid(pid);

		Category child = CommonUtils.toBean(req.getParameterMap(), Category.class);
		child.setCid(CommonUtils.uuid());
		child.setParent(parent);

		categoryService.addCategory(child);
		return findAll(req, resp);
	}

	/**
	 * 修改一级分类第一步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateOneLevelPre(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String cid = req.getParameter("cid");
		Category category = categoryService.findByCid(cid);
		req.setAttribute("category", category);
		req.setAttribute("cid", cid);
		return "f:/adminjsps/admin/category/edit.jsp";
	}

	/**
	 * 修改一级分类第二步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateOneLevel(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Category category = CommonUtils.toBean(req.getParameterMap(), Category.class);
		categoryService.updateCategory(category);
		return findAll(req, resp);
	}

	/**
	 * 修改二级分类第一步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateTwoLevelPre(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String cid = req.getParameter("cid");
		Category child = categoryService.findByCid(cid);
		req.setAttribute("child", child);

		Category parent = categoryService.findByCid(child.getParent().getCid());
		req.setAttribute("parent", parent);
		return "f:/adminjsps/admin/category/edit2.jsp";
	}

	/**
	 * 修改二级分类第二步
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateTwoLevel(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Category parent = new Category();
		String pid = req.getParameter("pid");
		parent.setCid(pid);

		Category child = CommonUtils.toBean(req.getParameterMap(), Category.class);
		child.setParent(parent);

		categoryService.updateCategory(child);
		return findAll(req, resp);
	}

	/**
	 * 删除分类
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteCategory(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String cid = req.getParameter("cid");
			Category category = categoryService.findByCid(cid);
			if (category != null && category.getParent() == null) { // 一级分类
				List<Category> childs = categoryService.findByParent(cid);
				if (childs.size() == 0) { // 没有子分类
					categoryService.deleteCategory(cid);
					return findAll(req, resp);
				} else { // 还有子分类
					req.setAttribute("code", "error");
					req.setAttribute("msg", "还有二级分类，不能删除此一级分类！");
					return "f:/adminjsps/msg.jsp";
				}
			} else { // 二级分类，直接删除
				categoryService.deleteCategory(cid);
				return findAll(req, resp);
			}
		} catch (SQLException e) {
			req.setAttribute("code", "error");
			req.setAttribute("msg", "此分类下还有BOOK，不能删除！");
			return "f:/adminjsps/msg.jsp";
		}
	}
}
